package com.ltsznh.android.familybooks.account;

import android.app.DialogFragment;
import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ltsznh.android.familybooks.R;
import com.ltsznh.android.familybooks.db.dbo;

/**
 * Created by Administrator on 13-9-18.
 */
public class AccountCategoryEditDialog extends DialogFragment {

    private Context ctx = null;
    private String tableName = "";
    private String whereClause = "";
    private String column = "";

    static AccountCategoryEditDialog newInstance(String editText) {
        AccountCategoryEditDialog fragment = new AccountCategoryEditDialog();
        Bundle args = new Bundle();
        args.putString("editText", editText);
        fragment.setArguments(args);
        return fragment;
    }

    public void setArgs(Context context,String tableName,String column, String whereClause){
        ctx = context;
        this.tableName = tableName;
        this.column = column;
        this.whereClause = whereClause;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int style = DialogFragment.STYLE_NORMAL, theme = android.R.style.Theme_Holo_Light_Dialog;
        setStyle(style, theme);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 填充对话框的UI
        final View view = inflater.inflate(R.layout.edit_dialog, container, false);
        //对话框标题
        getDialog().setTitle(R.string.title_activity_edit);
        // 更新对话框的内容
        TextView editTextView = (TextView)view.findViewById(R.id.textView_edit_dialog_text);
        editTextView.setText(getArguments().getString("editText"));

        final EditText editEditText = (EditText)view.findViewById(R.id.editText_edit_dialog_text);
        editEditText.setText(getArguments().getString("editText"));

        Button button = (Button)view.findViewById(R.id.button_edit_dialog_button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // When button is clicked, call up to owning activity.
                dbo.getWritableDatabase(ctx);
                ContentValues values = new ContentValues();
                values.put(column,editEditText.getText().toString());
                long i = dbo.updateData(ctx, tableName, values, whereClause, null);
                if(i >= 0){
                    Toast.makeText(ctx,getResources().getString(R.string.text_update_success),Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(ctx,"Error",Toast.LENGTH_SHORT).show();
                }
                dbo.closeDatabase();
                ((AccountCategoryActivity)getActivity()).refreshData();
//                ctx
                dismiss();
            }
        });

        return view;
    }
}
